﻿@using System.Text;
@{
     string custid = string.Empty;
     var custName = Request["custName"];
     if(custName!=""){
            custid = UtilityManager.GetVal(Request["custId"]);
            if(custid==""){
                custid="0";
            }
        }
     //var custid = UtilityManager.GetVal(Request["custId"]);
     var ccno = UtilityManager.GetVal(Request["ccno"]);
     var fc = UtilityManager.GetVal(Request["fieldChanged"]);
     var changeBy = UtilityManager.GetVal(Request["changedBy"]);
     var toc = UtilityManager.GetVal(Request["toc"]);
     var ov = UtilityManager.GetVal(Request["oldValue"]);
     var nv = UtilityManager.GetVal(Request["newValue"]);
     var dateFrom = UtilityManager.GetDateVal(Request["dateFrom"]);
     var dateTo = UtilityManager.GetDateVal(Request["dateTo"]);
    
     
    //var query = "SELECT * FROM AuditTrail WHERE DateCreated >= @0 AND DateCreated <= @1";
    var query = "SELECT * FROM AuditTrail ";
    StringBuilder sb = new StringBuilder();
    sb.Append(query);
    
    //check params
    int pCtr=0;
    Dictionary<string,dynamic> p = new Dictionary<string,dynamic>();
    if(custid!=""){
        p.Add("CustomerID",custid);
        pCtr++;
    }
    if(ccno!=""){
        p.Add("CashCardNo",ccno);
        pCtr++;
    }
    if(fc!=""){
        p.Add("FieldChange",fc);
        pCtr++;
    }
    if(changeBy!=""){
        p.Add("ChangeBy",changeBy);
        pCtr++;
    }
    if(toc!=""){
        p.Add("TypeOfChange",toc);
        pCtr++;
    }
    if(ov!=""){
        p.Add("OldValue",ov);
        pCtr++;
    }
    if(nv!=""){
        p.Add("NewValue",nv);
        pCtr++;
    }
    
    if(dateFrom!=null){
        p.Add("DateFrom",dateFrom);
        pCtr++;
    }
    
   /* if(dateTo!=null){
        p.Add("DateTo",dateTo);
        pCtr++;
    }*/
    
    if(pCtr>0){
        int ctr=0;
        string cond=string.Empty;
        foreach(KeyValuePair<string,dynamic> pair in p){
            if(ctr==0){
                cond =" WHERE";
            }else{
                cond=" AND";
            }
            if((string.Compare(pair.Key,"FieldChange")==0) || (string.Compare(pair.Key,"ChangeBy")==0) || (string.Compare(pair.Key,"TypeOfChange")==0)|| (string.Compare(pair.Key,"OldValue")==0) || (string.Compare(pair.Key,"NewValue")==0)){
                     sb.Append(cond).Append(string.Format(" {0} LIKE '{1}%'",pair.Key,pair.Value));
            }else if(string.Compare(pair.Key,"DateFrom")==0){
                 var newDF = Convert.ToDateTime(dateFrom);
                 var newDT = Convert.ToDateTime(dateTo);
              
                 sb.Append(cond).Append(string.Format(" DateCreated>='{0}' AND DateCreated<='{1}'",newDF,newDT.AddDays(1)));
            }else{
                sb.Append(cond).Append(string.Format(" {0}='{1}'",pair.Key,pair.Value));
            }
             ctr++;   
        }
    }
    
    
    var cols = new[]{"CashCardAppNo","FieldChange","TypeOfChange","OldValue","NewValue","ChangeBy","DateCreated"};
    
    var data = DBManager.Query(sb.ToString(),null);
   
    var grid = new WebGrid(data, columnNames: cols,canPage:true,canSort:true,defaultSort:"DateCreated",ajaxUpdateContainerId: "grid",ajaxUpdateCallback: "check");
    if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty()) {
        grid.SortDirection = SortDirection.Descending;
    }
    
    
}
<script type="text/javascript">
    $(function() {
        /*$('th a, tfoot a').live('click', function() {
        $('#searchResult').attr('action', $(this).attr('href')).submit();
        return false;
        });*/
        check();

        $('th a, tfoot a').live('click', function() {
            $(this).attr('href', '#grid-anchor');
        });
    });

    function check() {
        $("#genPDF").click(function() {
            genPDF();
        });
        $("#genXL").click(function() {
            genXL();
        });
    }
    function genPDF() {
        $("#auditForm").attr("action", "/Admin/GeneratePDF/");
        $("#auditForm").submit();
    }
    function genXL() {
        $("#auditForm").attr("action", "/Admin/GenerateExcel/");
        $("#auditForm").submit();
    }  
      
</script>
<style>
  



</style>
<div id="grid">
<fieldset>
        <legend>Search Results</legend>
<!--<form method="post" id="searchResult">
    <input type="hidden" name="dateFrom" value="@dateFrom"/>
    <input type="hidden" name="dateTo" value="@dateTo"/>-->
    
     <div id="buttons">
            <input type="button" id="genPDF" value="print to pdf"/>
            <input type="button" id="genXL" value="print to excel"/>
        </div>
<a name="grid-anchor"></a>    

     @functions{
    
         static IHtmlString ShowCustomer(string id){
                    var cust = CustomerManager.GetCustomer(id);
                    if(cust!=null){
                        return new HtmlString(cust.FName + " " + cust.LName);
                    }else{
                        return new HtmlString("");
                    }
                }
     
     }
     @grid.GetHtml(    
               tableStyle:"webgrid",
               alternatingRowStyle:"webgrid-alternating-row",
               headerStyle:"ui-widget-header",
               fillEmptyRows:true,
            columns: grid.Columns(
                grid.Column(
                    columnName:"CustomerID",
                    header:"Customer",
                    format:@<text>@ShowCustomer(item.CustomerID.ToString())</text>
                ),
                grid.Column("CashCardNo","CashCardNo"),
                grid.Column("FieldChange", "Field Changed"),
                grid.Column("TypeOfChange", "Type of Change"),
                grid.Column("OldValue", "Old Value"),
                grid.Column("NewValue","New Value"),
                grid.Column("ChangeBy","Changed By"),
                grid.Column("DateCreated","Date Changed")
                
               
            )
        )
</div>
    
<!--    </form> -->
</fieldset>    